package com.beiing.tianshuai.tianshuai.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.AppConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.PublishContentInfo;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.lang.reflect.InvocationTargetException;

import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Jinni on 2017/11/6 0006
 */

public class NewDiscoveryPublishContentListAdapter extends BaseRecyclerViewAdapter<PublishContentInfo> {

    private Context mContext;
    private OnItemDeleteListener mOnItemDeleteListener;
    private OnTxtClickListener mOnTxtClickListener;
    private OnImageViewClickListener mOnImageViewClickListener;

    public NewDiscoveryPublishContentListAdapter(Context context) {
        mContext = context;
    }

    /**
     * 设置条目删除的点击监听事件回调
     * @param onItemDeleteListener 删除点击监听者
     */
    public void setOnItemDeleteListener(OnItemDeleteListener onItemDeleteListener) {
        mOnItemDeleteListener = onItemDeleteListener;
    }

    /**
     * 设置文字条目的点击监听事件回调
     * @param onTxtClickListener 文字条目点击监听者
     */
    public void setOnTxtClickListener(OnTxtClickListener onTxtClickListener) {
        mOnTxtClickListener = onTxtClickListener;
    }

    /**
     * 设置图片条目的点击监听事件回调
     * @param onImageViewClickListener 图片条目点击监听者
     */
    public void setOnImageViewClickListener(OnImageViewClickListener onImageViewClickListener) {
        mOnImageViewClickListener = onImageViewClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if (!mList.isEmpty()) {
            int type = mList.get(position).getType();
            switch (type) {
                case AppConstant.ITEM_TYPE_TEXT:
                    return AppConstant.ITEM_TYPE_TEXT;
                case AppConstant.ITEM_TYPE_IMG:
                    return AppConstant.ITEM_TYPE_IMG;
                case AppConstant.ITEM_TYPE_VIDEO:
                    return AppConstant.ITEM_TYPE_VIDEO;
                case AppConstant.ITEM_TYPE_REPRINT:
                    return AppConstant.ITEM_TYPE_REPRINT;
            }
        }
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case AppConstant.ITEM_TYPE_TEXT:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_publish_text, parent, false);
                return new TextContentViewHolder(view);
            case AppConstant.ITEM_TYPE_IMG:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_publish_img, parent, false);
                return new ImageContentViewHolder(view);
            case AppConstant.ITEM_TYPE_VIDEO:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_publish_video, parent, false);
                return new VideoContentViewHolder(view);
            case AppConstant.ITEM_TYPE_REPRINT:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_publish_reprint, parent, false);
                return new ReprintContentViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        PublishContentInfo contentInfo = mList.get(position);
        if (holder instanceof TextContentViewHolder) {
            ((TextContentViewHolder) holder).setData(contentInfo);
        } else if (holder instanceof ImageContentViewHolder) {
            ((ImageContentViewHolder) holder).setData(contentInfo);
        } else if (holder instanceof VideoContentViewHolder) {
            ((VideoContentViewHolder) holder).setData(contentInfo);
        } else if (holder instanceof ReprintContentViewHolder) {
            ((ReprintContentViewHolder) holder).setData(contentInfo);
        }
    }

    /**
     * 文字内容的Holder
     */
    public class TextContentViewHolder extends RecyclerView.ViewHolder {

        /**
         * 文字内容
         */
        private WebView mWebView;

        /**
         * 删除按钮
         */
        private Button mBtnDel;

        /**
         * 遮罩层拦截点击事件
         */
        private FrameLayout mFrameLayout;

        @SuppressLint("SetJavaScriptEnabled")
        private TextContentViewHolder(View itemView) {
            super(itemView);
            mWebView = itemView.findViewById(R.id.item_tv_content);
            mBtnDel = itemView.findViewById(R.id.btn_delete);
            mFrameLayout = itemView.findViewById(R.id.content_container);
        }

        public void setData(final PublishContentInfo contentInfo) {
            // WebView
            WebSettings settings = mWebView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setSupportMultipleWindows(true);
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }
            String htmlText = "<html>" + "<head>" +
                    "<style type=\"text/css\">" + "body{padding: 0px;margin: 0px;}" + "</style>" + "</head>"
                    +contentInfo.getContent()+"<body></body></html>";
            mWebView.loadData(htmlText, "text/html; charset=UTF-8", null);

            mFrameLayout.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnTxtClickListener != null) {
                        mOnTxtClickListener.onEditTextListener(contentInfo, getAdapterPosition());
                    }
                }
            });

            // 删除
            mBtnDel.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnItemDeleteListener != null) {
                        mOnItemDeleteListener.onDeleteListener(contentInfo, getAdapterPosition());
                    }
                }
            });
        }
    }

    /**
     * 图片内容Holder
     */
    private class ImageContentViewHolder extends RecyclerView.ViewHolder {

        /**
         * 图片内容
         */
        private ImageView mImageView;

        /**
         * 菜单容器
         */
        private LinearLayout mMenuContainer;

        /**
         * 查看原图
         */
        private Button mBtnPreview;

        /**
         * 替换图片
         */
        private Button mBtnReplaceImg;
        /**
         * 图片剪裁
         */
        private Button mCropImg;
        /**
         * 图片删除
         */
        private Button mDeleteImg;

        @SuppressLint("SetJavaScriptEnabled")
        private ImageContentViewHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.item_iv_img);
            mMenuContainer = itemView.findViewById(R.id.ll_menu_container);
            mBtnPreview = itemView.findViewById(R.id.btn_preview_img);
            mBtnReplaceImg = itemView.findViewById(R.id.btn_replace_img);
            mCropImg = itemView.findViewById(R.id.btn_crop_img);
            mDeleteImg = itemView.findViewById(R.id.btn_delete_img);
        }

        public void setData(final PublishContentInfo contentInfo) {
            RequestOptions avatarOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(mContext.getApplicationContext())
                    .load(contentInfo.getSrc())
                    .apply(avatarOptions)
                    .into(mImageView);
            mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnImageViewClickListener != null) {
                        mOnImageViewClickListener.onImageViewClick(mMenuContainer, contentInfo, getAdapterPosition());
                    }
                }
            });

            mDeleteImg.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnImageViewClickListener != null) {
                        mOnImageViewClickListener.onDelClick(getAdapterPosition());
                    }
                }
            });

            mBtnReplaceImg.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnImageViewClickListener != null) {
                        mOnImageViewClickListener.onReplaceClick(mMenuContainer, contentInfo,
                                getAdapterPosition());
                    }
                }
            });

            mBtnPreview.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnImageViewClickListener != null) {
                        mOnImageViewClickListener.onImgPreview(mMenuContainer, contentInfo);
                    }
                }
            });

            mCropImg.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnImageViewClickListener != null) {
                        mOnImageViewClickListener.onImgCrop(mMenuContainer, contentInfo,
                                getAdapterPosition());
                    }
                }
            });
        }
    }

    /**
     * 视频内容Holder
     */
    private class VideoContentViewHolder extends RecyclerView.ViewHolder {

        /**
         * 视频展示WebView
         */
        private WebView mWebView;

        /**
         * 删除按钮
         */
        private Button mBtnDel;

        @SuppressLint("SetJavaScriptEnabled")
        private VideoContentViewHolder(View itemView) {
            super(itemView);
            mWebView = itemView.findViewById(R.id.item_wv);
            mBtnDel = itemView.findViewById(R.id.btn_delete);
        }

        public void setData(final PublishContentInfo contentInfo) {
            // WebView
            WebSettings settings = mWebView.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setSupportMultipleWindows(true);
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }
            String htmlText = "<html>" + "<head>" +
                    "<style type=\"text/css\">" + "body{padding: 0px;margin: 0px;}" + "</style>" + "</head>"
                    +contentInfo.getContent()+"<body></body></html>";
            mWebView.loadData(htmlText, "text/html; charset=UTF-8", null);
            if (contentInfo.isOnPause()) {
                try {
                    mWebView.getClass().getMethod("onResume").invoke(mWebView, (Object[]) null);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }

            // 删除按钮
            mBtnDel.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnItemDeleteListener != null) {
                        mOnItemDeleteListener.onDeleteListener(contentInfo, getAdapterPosition());
                    }
                }
            });
        }
    }

    /**
     * 转载链接内容Holder
     */
    private class ReprintContentViewHolder extends RecyclerView.ViewHolder {

        /**
         * 原著出处
         */
        private TextView mTextView;

        /**
         * 删除按钮
         */
        private Button mBtnDel;

        @SuppressLint("SetJavaScriptEnabled")
        private ReprintContentViewHolder(View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.item_tv_content);
            mBtnDel = itemView.findViewById(R.id.btn_delete);
        }

        public void setData(final PublishContentInfo contentInfo) {
            mTextView.setText(Html.fromHtml(contentInfo.getContent()));
            mBtnDel.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnItemDeleteListener != null) {
                        mOnItemDeleteListener.onDeleteListener(contentInfo, getAdapterPosition());
                    }
                }
            });
        }
    }

    /**
     * 删除按钮的监听事件
     */
    public interface OnItemDeleteListener{
        void onDeleteListener(PublishContentInfo bean, int position);
    }

    /**
     * 文字内容点击监听事件
     */
    public interface OnTxtClickListener{
        void onEditTextListener(PublishContentInfo bean, int position);
    }

    /**
     * 图片点击事件
     */
    public interface OnImageViewClickListener{
        void onImageViewClick(View view, PublishContentInfo bean, int position);
        void onDelClick(int position);
        void onReplaceClick(View view, PublishContentInfo contentInfo, int adapterPosition);
        void onImgPreview(View view, PublishContentInfo contentInfo);
        void onImgCrop(View view, PublishContentInfo contentInfo, int adapterPosition);
    }
}